package com.hui.utils;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

public class DESEncryptHelper {

    private final static String DES = "DES";

    /**
     * 生成密钥
     */

    public static String getDESKey(String encryptStr) {

        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();


        return key.toString();

    }

    public static void main(String[] args) {


        String content = "aq1sw2de";

        DES des1 = new DES("ECB", "PKCS7Padding", "zdwwyjcpm".getBytes());//key  zdwwyjcmp
        DES des2 = new DES(Mode.ECB, Padding.PKCS5Padding, "zdwwyjxgp".getBytes());//key  zdwwyjxgp

        String encrypt1 = des1.encryptHex(content);
        System.out.println("前端加密后的数据：" + encrypt1);

        String encrypt2 = des2.encryptHex(content);
        System.out.println("后端加密后的数据：" + encrypt2);

//
//        String decrypt1 = des2.decryptStr(encrypt2);
//        System.out.println("第一次解密后的数据：" + decrypt1);
//
//        String decrypt2 = des1.decryptStr(decrypt1);
//        System.out.println("第二次解密后的数据：" + decrypt2);


//        String content = "renrenio:12a00c04671504cc465a5124e9d9cdf5";
//
//        DES des1 = new DES(Mode.ECB, Padding.PKCS5Padding, "zdwwyjcpm".getBytes());
//
//        String encrypt1 = des1.encryptHex(content);
//        System.out.println("加密后的数据：" + encrypt1);
//
//        String decrypt1 = des1.decryptStr(encrypt1);
//        System.out.println("解密后的数据：" + decrypt1);


//        String content = "renrenio";
//        AES aes = new AES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud`".getBytes(), "0102030405060708".getBytes());
//        AES aes = new AES(Mode.ECB, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes());
//
//        // 加密为16进制表示
//        String encryptHex = aes.encryptHex(content);
//        System.out.println(encryptHex);
//        // 解密为字符串
//        String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
//        System.out.println(decryptStr);
//        String decryptStr = aes.decryptStr("renrenio", CharsetUtil.CHARSET_UTF_8);


    }


    /**
     * 对字符串加密
     *
     * @param str
     * @return
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws InvalidKeySpecException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     */

    public static String getEncryptStr(String str, String encryptStr) throws InvalidKeyException,

            IllegalBlockSizeException, BadPaddingException,

            InvalidKeySpecException, NoSuchAlgorithmException,

            NoSuchPaddingException {

//获取key

        String key = getDESKey(encryptStr);

//获取密钥

        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");

        DESKeySpec keyspec = new DESKeySpec(key.getBytes());

        SecretKey deskey = factory.generateSecret(keyspec);

// Cipher负责完成加密或解密工作

        Cipher c = Cipher.getInstance("DES");

// 根据密钥，对Cipher对象进行初始化，DECRYPT_MODE表示加密模式

        c.init(Cipher.ENCRYPT_MODE, deskey);

        byte[] src = str.getBytes();

// 该字节数组负责保存加密的结果

        byte[] cipherByte = c.doFinal(src);

        String enstr = new String(Base64.encodeBase64(cipherByte));

        return enstr;

    }

    /**
     * Description 根据键值进行解密
     *
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */

    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {

// 生成一个可信任的随机数源

        SecureRandom sr = new SecureRandom();

// 从原始密钥数据创建DESKeySpec对象

        DESKeySpec dks = new DESKeySpec(key);

// 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

        SecretKey securekey = keyFactory.generateSecret(dks);

// Cipher对象实际完成解密操作

        Cipher cipher = Cipher.getInstance(DES);

// 用密钥初始化Cipher对象

        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);

    }

}
